package work.comeback.omo.controller.servlet.movie;

import com.google.gson.Gson;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import work.comeback.omo.dao.movie.IMovieInfo;
import work.comeback.omo.model.http.ResponseData;
import work.comeback.omo.model.movie.info.MovieVoidData;
import work.comeback.omo.service.MovieService;
import work.comeback.omo.service.imp.MovieServiceImpl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@WebServlet("/api/movie/info")
public class MovieInfoServlet extends HttpServlet {
    private final MovieService movieService = new MovieServiceImpl();
    private final IMovieInfo movieInfo = movieService.getMovieInfo();
    private final Gson gson = new Gson();

    private void writeResponse(HttpServletResponse resp, ResponseData responseData) throws IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(gson.toJson(responseData));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String action = req.getParameter("action");
        String typeName = req.getParameter("typeName");
        String movieName = req.getParameter("movieName");

        try {
            if ("listAll".equals(action)) {
                int limit = Integer.parseInt(req.getParameter("limit"));
                Map<String, List<MovieVoidData>> movieList = movieInfo.getGroupedDataByTypeName(limit);
                writeResponse(resp, ResponseData.success("success", movieList));
            } else if ("listTypes".equals(action)) {
                List<String> typeNames = movieInfo.getAllTypeNames();
                writeResponse(resp, ResponseData.success("success", typeNames));
            } else if ("query".equals(action)) {
                if (movieName == null || movieName.isEmpty()) {
                    // 通过 ID 查询指定影片
                    int vodId = Integer.parseInt(req.getParameter("vodId"));
                    MovieVoidData movie = movieInfo.getMovieInfoById(vodId);
                    if (movie != null) {
                        writeResponse(resp, ResponseData.success("success", movie));
                    } else {
                        writeResponse(resp, ResponseData.fault("Movie not found", null));
                    }
                } else {
                    List<MovieVoidData> movieList = movieInfo.getMoviesByName(movieName);
                    writeResponse(resp, ResponseData.success("success", movieList));
                }
            } else if (typeName != null && !typeName.isEmpty()) {
                int limit = Integer.parseInt(req.getParameter("limit"));
                List<MovieVoidData> movieList = movieInfo.getMoviesByTypeName(typeName, limit);
                writeResponse(resp, ResponseData.success("success", movieList));
            } else {
                writeResponse(resp, ResponseData.fault("Invalid request parameters", null));
            }
        } catch (NumberFormatException e) {
            writeResponse(resp, ResponseData.fault("Invalid parameter", null));
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String vodIdStr = req.getParameter("vodId");
        String vodName = req.getParameter("vodName");
        String typeName = req.getParameter("typeName");
        String vodEn = req.getParameter("vodEn");
        String vodTime = req.getParameter("vodTime");
        String vodRemarks = req.getParameter("vodRemarks");

        try {
            int vodId = Integer.parseInt(vodIdStr);
            MovieVoidData movie = movieInfo.getMovieInfoById(vodId);
            if (movie != null) {
                movie.setVodName(vodName);
                movie.setTypeName(typeName);
                movie.setVodEn(vodEn);
                movie.setVodTime(vodTime);
                movie.setVodRemarks(vodRemarks);
                boolean updated = movieInfo.update(movie);
                writeResponse(resp, ResponseData.success("更新成功", updated));
            } else {
                writeResponse(resp, ResponseData.fault("电影不存在", null));
            }
        } catch (NumberFormatException e) {
            writeResponse(resp, ResponseData.fault("Invalid vodId parameter", null));
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String vodIdStr = req.getParameter("vodId");

        try {
            int vodId = Integer.parseInt(vodIdStr);
            boolean deleted = movieInfo.deleteById(vodId);
            writeResponse(resp, ResponseData.success("删除成功", deleted));
        } catch (NumberFormatException e) {
            writeResponse(resp, ResponseData.fault("Invalid vodId parameter", null));
        }
    }
}